//
//  DetailViewController.m
//  HelloWord
//
//  Created by David Zou on 09-1-8.
//  Copyright 2009 China Post. All rights reserved.
//

#import "DetailViewController.h"
#import "StudyViewController.h"
#import "ProVocWord.h"

@implementation DetailViewController
@synthesize cellText;

// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
		NSLog(@"show Detailview!");
		
		if(self.title == nil)
			self.title = @"Nil";
		
		self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
												   initWithTitle:@"Study" 
												   style:UIBarButtonItemStylePlain 
												   target:self 
												   action:@selector(BeginStudy:)]autorelease];
    }
    return self;
}

- (void)BeginStudy:(id)sender
{
	StudyViewController *studyView = [[StudyViewController alloc] initWithNibName:@"StudyView" bundle:nil];
	studyView.allWords = cellText;
	
	UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Back" style: UIBarButtonItemStyleBordered target: nil action: nil];
	
	[[self navigationItem] setBackBarButtonItem: newBackButton];
	
	[newBackButton release];
	[[self navigationController] pushViewController:studyView animated:YES];
	[studyView release];

	
}
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
		NSLog(@"show viewload!");
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	NSLog(@"show viewdidload!");
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.cellText count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	cell.textLabel.text = [[self.cellText objectAtIndex:[indexPath row]] sourceWord];
	cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
	cell.editingAccessoryType = UITableViewCellAccessoryNone;

    return cell;
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
	NSString *message = @"Alert";
	UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Have fun" 
												   message:message 
												  delegate:nil 
										 cancelButtonTitle:@"NO"
										 otherButtonTitles:nil];
	
	[alert show];
	[message release];
	[alert release];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (void)dealloc {
	[cellText release];
    [super dealloc];
}

@end
